# coding: utf-8

# In[1]:
import requests
import pandas as pd
import csv


# 读取之前获取的个股csv丢入到一个列表中
def getStockList():
    stockList = []
    f = open('num.csv', 'r', encoding='utf-8')
    f.seek(0)
    reader = csv.reader(f)
    for item in reader:
        stockList.append(item)
    f.close()
    return stockList

if __name__ == "__main__":
    stockList = getStockList()
    stockList.pop(0)
    for s in stockList:
        stock_code = str(s[2])
        stock_name = str(s[1])
        start_date = '20200614'
        end_date = '20210614'
        url = 'http://quotes.money.163.com/service/chddata.html?code={}&start={}&end={}'.format(stock_code, start_date,
                                                                                        end_date)
        # 从网上获取数据
        online_data = requests.get(url)
        csv_file_name = 'stock_{}.csv'.format(stock_name)

        # 将数据保存到本地csv文件
        with open(csv_file_name, "wb") as f:
            f.write(online_data.content)
        stock_data = pd.read_csv(csv_file_name, encoding='gb2312')
        stock_data = stock_data.sort_values(by=["日期"], ascending=[True], inplace=False)
